﻿

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/layui/layui.js"></script>
    <script src="~/Content/myjs/jquery-1.10.2.min.js"></script>
</head>
<body style="padding:40px 0 30px 80px;">
    <div class="layui-col-md8">
        <form class="layui-form layui-form-pane" lay-filter="form_add" @*action="/Role/Add"*@>
            <div class="layui-form-item">
                <label class="layui-form-label">角色编号</label>
                <div class="layui-input-inline">
                    <input type="text" name="RoleId" lay-verify="required||isrepeat" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="RoleName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">角色描述</label>
                <div class="layui-input-inline">
                    
                </div><textarea name="Description" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>
            </div>

            <div class="layui-form-item" style="text-align:center;margin:0 90px 0 0;">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">确认添加</button>
            </div>
        </form>
    </div>

    <script>

        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
            var layedit = layui.layedit

            //自定义验证规则
            form.verify({
                maxlength: function (value) {
                    if (value.length > 16) {
                        return '不能超过16个字符';
                    }
                }
                //判断用户账号是否存在
                , isrepeat: function (value) {
                    debugger;
                    var callbackdata;
                    $.ajax({
                        url: "/Role/CheckRepeat",
                        type: "post",
                        async: false,//同步
                        data: {
                            value: value
                        },
                        success: function (data) {
                            callbackdata = data;
                        }
                    });
                    if (callbackdata == "no") {
                        return ("同一用户名的信息已存在，请添加其他用户信息！");
                    }
                }
            });
            //监听提交
            form.on('submit(formDemo)', function (data) {
                debugger;
                $.ajax({
                    url: '/Role/Add',
                    method: 'post',
                    data: data.field,
                    success: function (data) {

                        if (data == "ok") {
                            layer.alert("添加成功！");
                            setTimeout(function () {
                                //休眠1秒后页面跳转
                                window.location.href = "/Role/AddIndex";
                                //关闭当前弹框
                                //var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                //parent.layer.close(index); //再执行关闭
                            }, 2000);


                        } else {
                            layer.alert("添加失败");
                        }
                    }
                });
                return false;//防止页面跳转
            });


        });
    </script>

</body>
</html>
